﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using TileMapEditor.TileEngine;

using XNAKey = Microsoft.Xna.Framework.Input.Keys;

namespace TileMapEditor
{
    class MapDisplay : WinFormsGraphicsDevice.GraphicsDeviceControl
    {
        public event EventHandler OnInitialize;
        public event EventHandler OnDraw;
    
        //Attributes
        private SpriteBatch spriteBatch;
        private ContentManager content;
        private Tile tile;
        private MouseState currentState;
        private MouseState prevState;
        private SpriteFont font;

        private Texture2D defaultTexture;
        private Texture2D blankTexture;
        private Texture2D grassTex;
        private List<Texture2D> textures;
        private int selectedID;

        private int defaultWidth;
        private int defaultHeight;
        private int defaultTileWidth;
        private int defaultTileHeight;

        private Camera camera;
        private Map map;

        public int SelectedID 
        {
            get 
            {
                return selectedID;
            }
            set 
            {
                selectedID = value;
            }
        }

        public Map Map 
        {
            get { return map; }
            set 
            {
                map = value;

                Layer layer = new Layer("default", defaultWidth,defaultHeight);

                for (int y = 0; y < defaultWidth; y++)
                {
                    for (int x = 0; x < defaultHeight; x++)
                    {
                        layer.Tiles[y, x] = new Tile(0, blankTexture,
                                                new Vector2(x * defaultTileWidth, y * defaultTileHeight));
                        layer.Tiles[y, x].Clicked += Tile_Clicked;
                    }
                }

                if (value != null)
                {
                    map.AddLayer(layer);
                    map.SetGrid(defaultTexture);
                }

            }
        }

        public MapDisplay()
        {
            prevState = Mouse.GetState();
        }


        protected override void Initialize()
        {
            if (OnInitialize != null)
                OnInitialize(this, null);

            content = new ContentManager(Services, "Content");
            spriteBatch = new SpriteBatch(GraphicsDevice);

            camera = new Camera(5.0f);

            font = content.Load<SpriteFont>("font");

            defaultTexture = content.Load<Texture2D>("gridDot");
            blankTexture = content.Load<Texture2D>("blankTile");
            grassTex = content.Load<Texture2D>("grassTile");
            textures = new List<Texture2D>();

            textures.Add(blankTexture); // 0
            textures.Add(grassTex);     // 1

            defaultWidth = 15;
            defaultHeight = 15;
            defaultTileWidth = 32;
            defaultTileHeight = 32;

            map = new Map(0,0,0,0);
            Map = new Map(defaultWidth,defaultHeight, defaultTileWidth, defaultTileHeight);

            //WHAT IS THIS?
            tile = new Tile(0, content.Load<Texture2D>("gridTile"), new Vector2(20, 20));
            tile.Clicked += Tile_Clicked;

            Application.Idle += delegate { Invalidate(); };
        }

        private void Tile_Clicked(object sender, EventArgs e)
        {
            Console.WriteLine("Tile was clicked");

            (sender as Tile).ChangeTile(textures[selectedID], selectedID);
        }

        protected override void Draw()
        {
            if (OnDraw != null)
                OnDraw(this, null);

            currentState = Mouse.GetState();

            GraphicsDevice.Clear(Color.AliceBlue);

            spriteBatch.Begin();

            //Layer.Draw  calls out the update and draw of all tiles on that layer

            map.Draw(spriteBatch, camera,currentState, prevState);

            //spriteBatch.DrawString(font, "MouseX: " + currentState.X + " MouseY: " + currentState.Y, new Vector2(100, 100), Color.White);

            spriteBatch.End();

            prevState = currentState;
        }

        public static Texture2D BitmapToTexture2D(GraphicsDevice device, System.Drawing.Bitmap image)
        {
            int bufferSize = image.Height * image.Width * 4;

            System.IO.MemoryStream memoryStream =
                new System.IO.MemoryStream(bufferSize);
            image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);

            Texture2D texture = Texture2D.FromStream(device, memoryStream);

            return texture;
        }
    }
}
